﻿#include "CrossEQ.h"
#include "CrossEQItem.h"
#include <QStyle>
#include <QGraphicsProxyWidget>

namespace GeneralUI {

	const double TEXT_WIDTH_RATIO = 0.135;

	CrossEQ::CrossEQ(QWidget *parent /*= nullptr*/)
		:UiWidgetBase(parent)
		, m_corssEQItem(nullptr)
		, m_bottomMsgLabel(nullptr)
		, m_rightMsgLabel(nullptr)
		, m_resetBtn(nullptr)
		, m_isShowText(false)
	{
		initUI();
		connect(m_corssEQItem, &CrossEQItem::signalValueChange, this, &CrossEQ::signalValueChange);
		connect(m_corssEQItem, &CrossEQItem::signalMouseRelease, this, [&]() {
			emit signalEditingFinished(getValue());
		});
	}

	CrossEQ::~CrossEQ()
	{
		if (m_corssEQItem != nullptr)
		{
			delete m_corssEQItem;
			m_corssEQItem = nullptr;
		}

		if (m_rightMsgLabel != nullptr)
		{
			delete m_rightMsgLabel;
			m_rightMsgLabel = nullptr;
		}

		if (m_bottomMsgLabel != nullptr)
		{
			delete m_bottomMsgLabel;
			m_bottomMsgLabel = nullptr;
		}

		if (m_resetBtn != nullptr)
		{
			delete m_resetBtn;
			m_resetBtn = nullptr;
		}
	}

	void CrossEQ::setFixedSize(int width, int height)
	{
		this->QWidget::setFixedSize(width, width);
		width -= 2;
		height -= 2;
		m_scene->setSceneRect(0, 0, width, width);
		if (m_isShowText)
		{
			double itemWidth = width * (1 - TEXT_WIDTH_RATIO);
			double btnWidth = width * TEXT_WIDTH_RATIO;
			m_corssEQItem->setItemSize(itemWidth - 1, itemWidth);
			m_rightMsgLabel->setGeometry(QRect(width, 0, itemWidth + 1, btnWidth + 2));
			m_bottomMsgLabel->setGeometry(QRect(0, itemWidth, itemWidth, btnWidth + 1));
			m_resetBtn->setGeometry(QRect(itemWidth + 1, itemWidth + 1, btnWidth, btnWidth));
		}
		else
		{
			m_corssEQItem->setItemSize(width, width);
		}

		m_resetBtn->setVisible(m_isShowText);
		m_bottomMsgLabel->setVisible(m_isShowText);
		m_rightMsgLabel->setVisible(m_isShowText);
	}

	void CrossEQ::setDecimals(int decs)
	{
		m_corssEQItem->setDecimals(decs);
	}

	void CrossEQ::setValue(double valueX, double valueY)
	{
		m_corssEQItem->setValue(valueX, valueY);
	}

	QPointF CrossEQ::getValue()
	{
		return m_corssEQItem->getValue();
	}

	void CrossEQ::setText(QString horText, QString verText)
	{
		m_rightMsgLabel->setText(verText);
		m_bottomMsgLabel->setText(horText);
	}

	void CrossEQ::setFont(QFont &font)
	{
		m_rightMsgLabel->setFont(font);
		m_bottomMsgLabel->setFont(font);
	}

	void CrossEQ::setShowText(bool isShowText)
	{
		m_isShowText = isShowText;
		setFixedSize(width(), height());
		update();
	}

	bool CrossEQ::isShowText()
	{
		return m_isShowText;
	}

	void CrossEQ::setRange(double min, double max)
	{
		m_corssEQItem->setRange(min, max);
	}

	void CrossEQ::setRange(double xMin, double xMax, double yMin, double yMax)
	{
		m_corssEQItem->setRange(xMin, xMax, yMin, yMax);
	}

	void CrossEQ::setPixStep(double step)
	{
		m_corssEQItem->setPixStep(step);
	}

	void CrossEQ::setBtnStyleSheet(QString styleSheet)
	{
		m_resetBtn->setStyleSheet(styleSheet);
	}

	bool CrossEQ::hasFocus()
	{
		return m_corssEQItem->hasFocus();
	}

	bool CrossEQ::blockSignals(bool block)
	{
		bool ret = m_corssEQItem->blockSignals(block);
		return ret;
	}

	void CrossEQ::setEnabled(bool isEnable)
	{
		m_corssEQItem->setEnabled(isEnable);
		m_rightMsgLabel->setEnabled(isEnable);
		m_bottomMsgLabel->setEnabled(isEnable);
		m_resetBtn->setEnabled(isEnable);
		return __super::setEnabled(isEnable);
	}

	void CrossEQ::initUI()
	{
		m_view = new QGraphicsView(this);
		m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		m_view->setBackgroundBrush(QBrush(QColor(245, 245, 245)));
		m_view->setStyleSheet("QGraphicsView{border:1px solid #CECECE;}");
		m_view->setRenderHint(QPainter::SmoothPixmapTransform);
		m_scene = new QGraphicsScene(m_view);
		m_view->setScene(m_scene);

		m_corssEQItem = new CrossEQItem(this);
		m_scene->addItem(m_corssEQItem);

		m_rightMsgLabel = new QLabel("crossEQ");
		m_rightMsgLabel->setAlignment(Qt::AlignCenter);
		m_rightMsgLabel->setStyleSheet(".QLabel{"
			"background-color:white;"
			"border:1px solid #CECECE;"
			"border-top:none;"
			"border-left:none;}");
		QGraphicsProxyWidget *rightMsgLabel = m_scene->addWidget(m_rightMsgLabel);
		rightMsgLabel->setRotation(90);

		m_bottomMsgLabel = new QLabel("crossEQ");
		m_bottomMsgLabel->setAlignment(Qt::AlignCenter);
		m_bottomMsgLabel->setStyleSheet(".QLabel{"
			"background-color:white;"
			"border-top:1px solid #CECECE;"
			"border-right:1px solid #CECECE;}");
		QGraphicsProxyWidget *bottomMsgLabel = m_scene->addWidget(m_bottomMsgLabel);

		m_resetBtn = new QPushButton();
		m_scene->addWidget(m_resetBtn);
		m_resetBtn->setObjectName("crossEqResetBtn");
		m_resetBtn->setProperty("type", "CrossEQReset");
		m_resetBtn->style()->unpolish(m_resetBtn);
		m_resetBtn->style()->polish(m_resetBtn);
		m_resetBtn->setCursor(Qt::PointingHandCursor);
		connect(m_resetBtn, &QPushButton::clicked, this, &CrossEQ::signalResetBtnClicked);

		setFixedSize(135, 135);
	}

}
